package com.grechur.wanandroidcompose

import android.os.Bundle
import android.util.Base64
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.view.WindowCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.grechur.wanandroidcompose.page.*
import com.grechur.wanandroidcompose.ui.theme.WanAndroidComposeTheme
import com.grechur.wanandroidcompose.viewmodel.CoinViewModel

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        var statusBarHeight = 0
//        var resourceId = resources.getIdentifier("status_bar_height","dimen","android")
//        if (resourceId > 0) {
//            statusBarHeight = resources.getDimensionPixelSize(resourceId)
//        }

//        window.statusBarColor = Color.Transparent.value.toInt()
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
//
//        } else {
//            window.decorView.systemUiVisibility =
//                View.SYSTEM_UI_FLAG_FULLSCREEN or
//                        View.SYSTEM_UI_LAYOUT_FLAGS
//        }

//        使页面显示在statusbar和bottombar后面，部分手机显示异常
        WindowCompat.setDecorFitsSystemWindows(window, false)

        setContent {
            WanAndroidComposeTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background) {
                    val globalNavController = rememberNavController()
                    val coinViewModel:CoinViewModel = viewModel()
                    NavHost(navController = globalNavController, startDestination = "home") {

                        composable("home") {
                            MainPage(globalNavController, coinViewModel = coinViewModel)
                        }
//                      深层链接模式，使用/作为分割，容易出现传递的内容中有/导致导航失败
//                        composable("webView/{url}/{name}",arguments = listOf(
//                                navArgument("url") {},
//                                navArgument("name") {}
//                        )) {
//                            val argument = it.arguments
//                            val url = argument?.getString("url")
//                            val name = argument?.getString("name")
//                            val realUrl = URLDecoder.decode(url, StandardCharsets.UTF_8.toString())
//                            val realName = String(Base64.decode(name,Base64.NO_WRAP))
//                            WebViewPage(globalNavController,realUrl,realName)
//                        }
                        composable("webView?url={url}?name={name}", arguments = listOf(
                            navArgument("url") {
                                type = NavType.StringType
                                defaultValue = ""
                            },
                            navArgument("name") {
                                type = NavType.StringType
                                defaultValue = ""
                            }
                        )) {
                            val argument = it.arguments
                            val url = argument?.getString("url")
                            var name = argument?.getString("name")
                            Log.e("zhouzhu","url:${url}")
                            val realUrl = String(Base64.decode(url, Base64.NO_WRAP))
                            Log.e("zhouzhu","realUrl:${realUrl}")
                            WebViewPage(globalNavController, realUrl, name)
                        }

                        composable("login") { LoginPage(globalNavController) }

                        composable("register") { RegisterPage(globalNavController) }

                        composable("search") { SearchPage(globalNavController) }

                        composable("commonWeb") { CommonWebPage(globalNavController) }

                        composable("mineCoin") { MineCoinPage(globalNavController, coinViewModel = coinViewModel) }

                        composable("coinRanking") { CoinRankPage(globalNavController, coinViewModel = coinViewModel) }
                    }
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    WanAndroidComposeTheme {
        Greeting("Android")
    }
}